# https://m.weibo.cn/p/index?containerid=2304444284583299200682&url_type=39&object_type=video&pos=1&luicode=10000011&lfid=1076033669102477
from bs4 import BeautifulSoup
import requests
from urllib.parse import unquote
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import json
import time
headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
    'Host': 'm.weibo.cn',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0'
    # 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36'
}
login_headers = {
    # 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept': 'application/json, text/plain, */*',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
    'Cache-Control': 'max-age=0',
    'Connection': 'keep-alive',
    'Host': 'm.weibo.cn',
    'mweibo-pwa': '1',
    'origin': 'https://m.weibo.cn',
    'x-requested-with': 'XMLHttpRequest',
    'Referer': 'https://m.weibo.cn/u/3669102477',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0',
    'Cookie': '_T_WM=0066bbafbec70e4b74b6ff88f78957c0; hibext_instdsigdipv2=1; from=1110006030; WEIBOCN_FROM=1110006030; MLOGIN=1; M_WEIBOCN_PARAMS=lfid%3D102803%26luicode%3D20000174%26fid%3D1005053669102477%26uicode%3D10000011; SUB=_2A252mnLODeRhGeBI7lQS8SzOwj2IHXVSZR6GrDV6PUJbkdANLWHFkW1NRpVacR40_3yHTfIjsjYTfqN71FsZ1QM3; SUHB=079j8qxb09Qc_T; SCF=AsfcBVjiz0kcskVtMDqUdIezE3sxu6PbZHnm6NGIZSsxapLvtpOzc8_9QYkVYCmqAfw8AYtorZOYV_p0p5RQAmg.; SSOLoginState=1537082014'
    # 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36'
}

# session = requests.Session()
# r = session.get('https://m.weibo.cn/p/index?containerid=23044404c2092c7cdb44120d942915e3140eec&url_type=39&object_type=video&pos=2&luicode=10000011&lfid=1076033669102477')
# # r = session.get('https://m.weibo.cn/api/container/getIndex?containerid=2304444284583299200682&url_type=39&object_type=video&pos=1&luicode=10000011&lfid=1076033669102477')
# print(r.text)
# jsonObj = r.json()
# next_url = jsonObj['data']['cards'][0]['content_url']
# print(next_url)

# firefox_options = Options()
# firefox_options.add_argument('--headless')
# firefox_options.add_argument('--disable-gpu')
# driver = webdriver.Firefox(executable_path=r'C:\Program Files\Mozilla Firefox\geckodriver.exe', options=firefox_options)
#
# driver.get(
#     'https://m.weibo.cn/p/index?containerid=23044404c2092c7cdb44120d942915e3140eec&url_type=39&object_type=video&pos=2&luicode=10000011&lfid=1076033669102477')
# try:
#     ele = WebDriverWait(driver, 15).until(
#         EC.presence_of_element_located((By.CSS_SELECTOR, '[node-type="common_video_player"]')))
# try:
#     ele = WebDriverWait(driver, 15).until(
#         EC.presence_of_element_located((By.CSS_SELECTOR, '[node-type="common_video_player"]')))
#     video_source = driver.find_element_by_css_selector('[node-type="common_video_player"]').get_attribute(
#         'video-sources')
#     cookie = driver.get_cookies()
#     print(cookie)
# finally:
#     driver.close()
# print(driver.page_source)
# video_url = unquote(video_source).split('720=')[-1]
# video_file_name = video_url.split('?')[0].split('/')[-1]
# video = requests.get(video_url)
# with open(video_file_name, 'wb') as video_file:
#     video_file.write(video.content)
session = requests.Session()
r = session.get('https://m.weibo.cn/p/index?containerid=23044404c2092c7cdb44120d942915e3140eec&url_type=39&object_type=video&pos=2&luicode=10000011&lfid=1076033669102477', headers=login_headers)
time.sleep(5)
print(r.url)
